package scau.mis4.chanyulin.web;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ExtendedModelMap;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import scau.mis4.chanyulin.base.common.BaseController;
import scau.mis4.chanyulin.base.utils.PasswordHelper;
import scau.mis4.chanyulin.entity.Employee;
import scau.mis4.chanyulin.service.IEmployeeService;
import java.util.List;

/**
 * Created by cyl on 2018/03/27.
 * Employee Controller层
 */
@RestController
@RequestMapping("/employee")
public class EmployeeController extends BaseController {

    @Autowired
    private IEmployeeService employeeService;

    /**
     * 添加Employee
     * @param employee
     * @return
     * @throws Exception
     */
    @PostMapping("/add")
    @RequiresRoles("0")
    public String addEmployee(Employee employee) throws Exception {
        employeeService.add(employee);
        return createSuccessJSON();
    }

    /**
     * 修改某个员工的权限等级
     * @param id
     * @param authLevel
     * @return
     * @throws Exception
     */
    @PostMapping("/update_auth")
    @RequiresRoles("0")
    public String updateAuthLevel(String id, Integer authLevel) throws Exception {
        Employee employee = employeeService.findByKey(id);
        employee.setAuthLevel(authLevel);
        employeeService.update(employee);
        return createSuccessJSON();
    }

    /**
     * 删除某个Employee
     * @param id
     * @return
     * @throws Exception
     */
    @PostMapping("/delete")
    @RequiresRoles("0")
    public String deleteEmployeeById(String id) throws Exception {
        employeeService.delete(id);
        return createSuccessJSON();
    }

    /**
     * 查找某个Employee
     * @param id
     * @return
     * @throws Exception
     */
    @GetMapping("/get")
    public String getEmployeeById(String id) throws Exception {
        Employee employee = employeeService.findByKey(id);
        Model model = new ExtendedModelMap().addAttribute("result", employee);
        return createSuccessJSON(model);
    }

    /**
     * 获取全部Employee
     * @return
     * @throws Exception
     */
    @GetMapping("/list")
    public String listAllEmployee() throws Exception {
        List<Employee> employeeList = employeeService.findAll();
        Model model = new ExtendedModelMap().addAttribute("result",employeeList);
        return createSuccessJSON(model);
    }

    /**
     * 分页查询Employee
     * @param pageNum 页码
     * @param pageSize 每页数据数量
     * @return
     * @throws Exception
     */
    @PostMapping("/page")
    public String listEmployeeByPage(int pageNum, int pageSize) throws Exception {
        PageHelper.startPage(pageNum, pageSize);
        List<Employee> employeeList = employeeService.findAll();
        PageInfo<Employee> pageInfo = new PageInfo<>(employeeList);
        Model model = new ExtendedModelMap()
                .addAttribute("result", employeeList)
                .addAttribute("pageInfo", pageInfo);
        return createSuccessJSON(model);
    }

    /**
     * 更新某个Employee
     * @param employee
     * @return
     * @throws Exception
     */
    @PostMapping("/update")
    @RequiresRoles("0")
    public String updateEmployeeById(Employee employee) throws Exception {
        List<Employee> employeeList = employeeService.findByProperties(employee.getName(), "name");
        if (employeeList.size() == 0) return createErrorJSON();
        Employee target = employeeList.get(0);
        target.setName(employee.getName());
        target.setJobTitle(employee.getJobTitle());
        target.setAuthLevel(employee.getAuthLevel());
        target.setGender(employee.getGender());
        if (null != employee.getPassword()) {
            if (!employee.getPassword().equals("")) {
                target.setPassword(employee.getPassword());
                PasswordHelper.encryptPassword(target);
            }
        }
        return createJSONByUpdates(employeeService.updateById(target));
    }

}
